package controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import pojo.CommentArray;
import pojo.Comments;
import service.CommentArrayService;
import service.CommentsService;
import service.impl.CommentArrayServiceImpl;
import service.impl.CommentsServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;

@WebServlet("/commentsServlet")
public class CommentsServlet extends BaseServlet {
    CommentsService commentsService = new CommentsServiceImpl();
    CommentArrayService commentArrayService = new CommentArrayServiceImpl();

    /**
     * 笔记评论
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void comments(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, SQLException, ClassNotFoundException {
        int nid = Integer.parseInt(req.getParameter("nid"));
        ArrayList<CommentArray> commentWithChilden = new ArrayList<>();
        //父级评论
        ArrayList<Comments> commentsList = new ArrayList<Comments>();
        commentsList = commentsService.findCommentByNid(nid);
        resp.setContentType("text/javascript");

        //子级评论
//        int cid=0;
//        for (Comments comments : commentsList) {
//            CommentArray commentArray=new CommentArray();
//            commentArray.setComments(comments);
//            cid=comments.getCid();
//            //该父评论的子评论
//            commentArray.setChildComments(commentArrayService.findCommentChild(cid));
//            commentWithChilden.add(commentArray);
//        }
        // System.out.println(commentWithChilden);


        ObjectMapper mapper = new ObjectMapper();
        mapper.writeValue(resp.getOutputStream(), commentsList);
        //mapper.writeValue(resp.getOutputStream(), commentWithChilden);
    }

    /**
     * 笔记子级评论
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void commentsChilden(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, SQLException, ClassNotFoundException {
        int cid = Integer.parseInt(req.getParameter("cid"));
        //System.out.println(cid);

        ArrayList<Comments> commentsChilden = commentArrayService.findCommentChild(cid);
        //子级评论
        resp.setContentType("text/javascript");
//        for (Comments comments : commentsList) {
//            CommentArray commentArray=new CommentArray();
//            commentArray.setComments(comments);
//            cid=comments.getCid();
//            //该父评论的子评论
//            commentArray.setChildComments(commentArrayService.findCommentChild(cid));
//            commentWithChilden.add(commentArray);
//        }
        // System.out.println(commentWithChilden);
        //System.out.println(commentsChilden);

        ObjectMapper mapper = new ObjectMapper();
        mapper.writeValue(resp.getOutputStream(), commentsChilden);
        //mapper.writeValue(resp.getOutputStream(), commentWithChilden);
    }


    /**
     * 添加评论
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected String addComment(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, SQLException {
        req.setCharacterEncoding("utf-8");
        int uid = Integer.parseInt(req.getParameter("uid"));
        int nid = Integer.parseInt(req.getParameter("nid"));
        String userComment = req.getParameter("comment");
        Comments comments = new Comments(uid, nid, userComment, null, 0, 0);
        boolean flag = false;
        flag = commentsService.addComment1(comments);
        req.setAttribute("nid", nid);
        req.setAttribute("uid", uid);
        if (flag == true)
//            req.getRequestDispatcher("/note.jsp").forward(req, resp);
            return "g_/note.jsp";
        else return "s_error.jsp";
    }

    /**
     * 删除评论
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected String userDeleteComment(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, SQLException, ClassNotFoundException {
        req.setCharacterEncoding("utf-8");
        int cid = Integer.parseInt(req.getParameter("cid"));
        //System.out.println(cid);
        Comments comments = new Comments();
        comments = commentsService.findCommentByCid(cid);
        //System.out.println(comments);
        int nid = comments.getNid();
        //System.out.println(nid);
        int uid = comments.getUid();
        //System.out.println(uid);
        boolean flag = false;
        flag = commentsService.userDeleteComment(cid);
        //System.out.println(flag);
        req.setAttribute("nid", nid);
        req.setAttribute("uid", uid);
        if (flag == true)
//            req.getRequestDispatcher("/note.jsp?nid=" + nid + "&uid=" + uid).forward(req, resp);
            return "/note.jsp?nid=" + nid + "&uid=" + uid;
        else return "s_error.jsp";
    }

}
